﻿//创建时间: 2010-10-11
//创建作者: 李 城
//功能说明: 对xml文件操作,适用于xml结点操作
//最后修改: 2010-10-11
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;
using System.IO;
using System.Collections;


namespace TRPOP.Common
{
    /// <summary>
    /// 对xml文件操作,适用于xml结点操作
    /// </summary>
    public class ConfigManage
    {
        /// <summary>
        /// 根据路径获取XML信息
        /// </summary>
        /// <param name="strPath"></param>
        /// <returns></returns>
        public static string GetXMLInfo(string strPath)
        {
            return strPath;
            //StreamReader sreader = new StreamReader(strPath, Encoding.UTF8);
            //string xml = sreader.ReadToEnd();
            //sreader.Close();
            //return xml;
        }

        /// <summary>
        ///  根据结点名 获取xml文件结点值(InnerText)
        /// </summary>
        /// <param name="Target">结点名</param>
        /// <param name="XmlPath">文件路径</param>
        /// <returns></returns>
        public static string GetConfigOneValue(string Target, string XmlPath)
        {
            System.Xml.XmlDataDocument xdoc = new XmlDataDocument();
            xdoc.Load(GetXMLInfo(XmlPath));
            XmlElement root = xdoc.DocumentElement;
            XmlNodeList elemList = root.GetElementsByTagName(Target);
            try
            {
                return elemList[0].InnerText;
            }
            catch
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 根据结点名 获取xml文件结点值(InnerText)
        /// </summary>
        /// <param name="Target">结点名</param>
        /// <param name="strInnertext">节点值</param>
        /// <param name="XmlPath">文件路径</param>
        public static void SaveConfigOneValue(string Target,string strInnertext, string XmlPath)
        {
            System.Xml.XmlDataDocument xdoc = new XmlDataDocument();
            xdoc.Load(GetXMLInfo(XmlPath));
            XmlElement root = xdoc.DocumentElement;
            XmlNodeList elemList = root.GetElementsByTagName(Target);
            try
            {
                elemList[0].InnerText = strInnertext;
                xdoc.Save(XmlPath);
            }
            catch
            {
                return;
            }
        }

        /// <summary>
        /// 获取某xml结点下所有的结点内容
        /// </summary>
        /// <param name="Key">结点key</param>
        /// <param name="XmlPath">xml文件地址</param>
        /// <returns>返回的结点内容集合，按照结点的先后顺序</returns>
        public static ArrayList GetValue(string Key, string XmlPath)
        {
            XmlDataDocument objXmlDoc = new XmlDataDocument();
            ArrayList oArrayList = new ArrayList();
            objXmlDoc.Load(GetXMLInfo(XmlPath));
            XmlNodeList nodelist = objXmlDoc.SelectNodes("//items/item");
            for (int i = 0; i < nodelist.Count; i++)
            {
                if (nodelist.Item(i).ChildNodes.Item(0).InnerText.Trim().ToLower() == Key.ToLower())
                {
                    foreach (XmlNode NodeOne in nodelist.Item(i).ChildNodes)
                    {
                        oArrayList.Add(((XmlElement)NodeOne).InnerText.Trim());
                    }
                    return oArrayList;
                }
            }
            return null;
        }

        /// <summary>
        /// 根据内容、名称集合 新增或修改xml信息
        /// </summary>
        /// <param name="ArrayName">结点名称集合</param>
        /// <param name="ArrayValue">结点内容集合</param>
        /// <param name="XmlPath">xml文件路径</param>
        /// <returns></returns>
        public static bool SaveConfigValue(ArrayList ArrayName, ArrayList ArrayValue, string XmlPath)
        {

            XmlDataDocument objXmlDoc = new XmlDataDocument();
            objXmlDoc.Load(GetXMLInfo(XmlPath));
            if (GetValue(ArrayValue[0].ToString(), XmlPath) == null)
            {
                //新增
                XmlNode objNode = objXmlDoc.SelectSingleNode("//items");
                XmlElement objParent = objXmlDoc.CreateElement("item");
                objNode.AppendChild(objParent);
                for (int i = 0; i < ArrayName.Count; i++)
                {
                    XmlElement IdElement = objXmlDoc.CreateElement(ArrayName[i].ToString().Trim());
                    IdElement.InnerText = ArrayValue[i].ToString().Trim();
                    objParent.AppendChild(IdElement);
                }
                objXmlDoc.Save(XmlPath);
                return true;
            }
            else
            {
                //修改
                XmlNodeList nodelist = objXmlDoc.SelectNodes("//items/item");
                for (int i = 0; i < nodelist.Count; i++)
                {
                    if (nodelist.Item(i).ChildNodes.Item(0).InnerText.Trim() == ArrayValue[0].ToString().Trim())
                    {
                        for (int j = 0; j < nodelist.Item(i).ChildNodes.Count && j < ArrayValue.Count; j++)
                        {
                            nodelist.Item(i).ChildNodes[j].InnerText = ArrayValue[j].ToString().Trim();
                        }
                        objXmlDoc.Save(XmlPath);
                        return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// 根据key删除结点
        /// </summary>
        /// <param name="ArrayKey"></param>
        /// <param name="XmlPath"></param>
        /// <returns></returns>
        public static bool DelConfigValue(ArrayList ArrayKey, string XmlPath)
        {
            bool oboolDel = false;
            XmlDataDocument objXmlDoc = new XmlDataDocument();
            objXmlDoc.Load(GetXMLInfo(XmlPath));
            XmlNodeList nodelist = objXmlDoc.SelectNodes("//items/item");
            for (int i = 0; i < nodelist.Count; i++)
            {
                for (int j = 0; j < ArrayKey.Count; j++)
                {
                    if (nodelist.Item(i).ChildNodes.Item(0).InnerText.Trim() == ArrayKey[j].ToString().Trim())
                    {
                        objXmlDoc.SelectSingleNode("//items").RemoveChild(nodelist.Item(i));
                        oboolDel = true;
                    }
                }
            }
            objXmlDoc.Save(XmlPath);
            return oboolDel;
        }

        /// <summary>
        /// 返回xml结点下所有数据集合
        /// </summary>
        /// <param name="XmlPath">xml文件路径</param>
        /// <param name="NodeListPath">结点路径</param>
        /// <returns></returns>
        public static DataTable GetDataSource(string XmlPath, string NodeListPath)
        {
            System.Xml.XmlDataDocument xdoc = new XmlDataDocument();
            xdoc.Load(GetXMLInfo(XmlPath));
            XmlNodeList objNode = xdoc.SelectNodes(NodeListPath);
            if (objNode.Count == 0)
                return null;
            DataSet ds = new DataSet();
            StringReader read = new StringReader(xdoc.SelectSingleNode(NodeListPath).OuterXml);
            ds.ReadXml(read);
            try
            {
                return ds.Tables[0];
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 通过XPath获取结点值
        /// </summary>
        /// <param name="XmlPath">xml文件路径</param>
        /// <param name="XPath">xpath</param>
        /// <returns></returns>
        public static string GetValueByXPath(string XmlPath, string XPath)
        {
            string result = "";
            System.Xml.XmlDataDocument xdoc = new XmlDataDocument();
            try
            {
                xdoc.Load(GetXMLInfo(XmlPath));
                XmlElement root = xdoc.DocumentElement;
                XmlNodeList nodeList=  root.SelectNodes(XPath);
                if (nodeList.Count > 0)
                {
                    result = nodeList[0].InnerText;
                }
            }
            catch
            {
                result = "";
            }
            return result;
        
        }


    }
}
